package com.blockmeta.bbs.overallserviceapplication.activity;

import androidx.viewpager.widget.PagerAdapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blockmeta.bbs.baselibrary.utils.LoadPicUtil;
import com.blockmeta.bbs.baselibrary.utils.OpenFileUtil;
import com.blockmeta.bbs.baselibrary.utils.permission.RequestPermissionUtil;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.overallserviceapplication.R;
import com.blockmeta.bbs.overallserviceapplication.R2;
import com.blockmeta.bbs.overallserviceapplication.widget.PicViewPager;
import com.github.chrisbanes.photoview.PhotoView;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by author on 2018/3/20.
 */
@Route(path = RouterPath.VIEWPAGER_ACTIVITY)
public class PictureViewerActivity extends BaseActivity {
    @BindView(R2.id.view_pager)
    PicViewPager mViewPager;
    @Autowired(name = Constant.ZERO)
    public ArrayList<String> mPics;
    @Autowired(name = Constant.ONE)
    public int currentPosition = 0;


    @Override
    protected boolean showTitle() {
        return true;
    }

    @Override
    public View setView() {
        setTitleText("浏览图片");
        return LayoutInflater.from(mContext).inflate(R.layout.activity_view_pager, null);
    }

    @Override
    protected void initialize() {
        super.initialize();
        //如果通过intent传递的图片地址为空，表示是通过静态变量传值，因为intent传值有限
        if(mPics == null || mPics.size() == 0){
            mPics = Constant.TempImgUrls;
            Constant.TempImgUrls = null;
        }
        mViewPager.setAdapter(new SamplePagerAdapter());
        mViewPager.setCurrentItem(currentPosition, false);
    }

    class SamplePagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return mPics==null?0:mPics.size();
        }

        @Override
        public View instantiateItem(ViewGroup container, final int position) {
            ImageView photoView = new PhotoView(container.getContext());
            if(TextUtils.isEmpty(mPics.get(position))){
                container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                return photoView;
            }
            if("gif".equals(OpenFileUtil.getExtensionName(mPics.get(position)).toLowerCase())){
                photoView = new ImageView(container.getContext());
                container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            }else {
                container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//                container.addView(photoView, DimentionsUtil.dp2px(120), DimentionsUtil.dp2px(200));
            }

            if (!PictureViewerActivity.this.isFinishing()) {
                LoadPicUtil.loadWithSourceStrategy(mPics.get(position),photoView);
                photoView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(final View view) {

                        RequestPermissionUtil.requestStorage(new RequestPermissionUtil.OnPermissionGrantedListener() {
                                                            @Override
                                                            public void onPermissionGranted() {

                                                                new Thread(new Runnable() {
                                                                    @Override
                                                                    public void run() {
                                                                        String path = LoadPicUtil.getImagePath(mPics.get(position), view.getContext());
                                                                        /**
                                                                         * 拷贝到指定路径
                                                                         */
                                                                        LoadPicUtil.savePictureFromGlide(path, OpenFileUtil.getExtensionName(mPics.get(position)), view.getContext());

                                                                    }
                                                                }).start();
                                                            }

                                                        }
                        );
                        return true;
                    }


                });
            }
            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }

}
